package com.zsx.shopping.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsx.shopping.common.api.coupon.vo.SeckillSessionVO;
import com.zsx.shopping.common.api.coupon.vo.SeckillSkuVO;
import com.zsx.shopping.coupon.entity.SeckillSession;
import com.zsx.shopping.coupon.entity.SeckillSessionSku;
import com.zsx.shopping.coupon.mapper.SeckillSessionSkuMapper;
import com.zsx.shopping.coupon.service.SeckillSessionService;
import com.zsx.shopping.coupon.mapper.SeckillSessionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionMapper, SeckillSession>
        implements SeckillSessionService {

    @Resource
    private SeckillSessionMapper seckillSessionMapper;

    @Resource
    private SeckillSessionSkuMapper seckillSessionSkuMapper;

    @Override
    public List<SeckillSessionVO> getLatest3DaySession() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        String start = LocalDateTime.of(now, min).format(formatter);

        LocalDate plus = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        String end = LocalDateTime.of(plus, max).format(formatter);

        List<SeckillSession> seckillSessions = seckillSessionMapper.selectList(new LambdaQueryWrapper<SeckillSession>()
                .between(SeckillSession::getStartTime, start, end));

        return seckillSessions.stream().map(seckillSession -> {
            SeckillSessionVO seckillSessionVO = new SeckillSessionVO();
            seckillSessionVO.setId(seckillSession.getId());
            seckillSessionVO.setEndTime(seckillSession.getEndTime());
            seckillSessionVO.setName(seckillSession.getName());
            seckillSessionVO.setStartTime(seckillSession.getStartTime());
            // 该方法用于秒杀商品上架,不需要高性能,因此这里随便用循环查询了
            List<SeckillSkuVO> seckillSkus = seckillSessionSkuMapper.selectList(new LambdaQueryWrapper<SeckillSessionSku>()
                    .eq(SeckillSessionSku::getSessionId, seckillSession.getId())).stream().map(seckillSessionSku -> {
                SeckillSkuVO seckillSkuVO = new SeckillSkuVO();
                seckillSkuVO.setId(seckillSessionSku.getId());
                seckillSkuVO.setSessionId(seckillSessionSku.getSessionId());
                seckillSkuVO.setSeckillCount(seckillSessionSku.getSeckillCount());
                seckillSkuVO.setSeckillLimit(seckillSessionSku.getSeckillLimit());
                seckillSkuVO.setSeckillPrice(seckillSessionSku.getSeckillPrice());
                seckillSkuVO.setSkuId(seckillSessionSku.getSkuId());
                return seckillSkuVO;
            }).collect(Collectors.toList());
            seckillSessionVO.setSeckillSkus(seckillSkus);
            return seckillSessionVO;
        }).collect(Collectors.toList());
    }
}




